﻿using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Reg;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System;
using System.Drawing;
using Tesseract;
using static Emgu.CV.Ccm.ColorCorrectionModel;

namespace EMguConsole.Core
{
    public class FindDigit
    {
        public static string path = @"C:\Users\93143\Desktop\2.jpg";
        public static void Find()
        {
            // 读取图像
            Mat imageBase = CvInvoke.Imread(path, ImreadModes.Color);

            // 去除噪声
            Mat image = new Mat();
            CvInvoke.MedianBlur(imageBase, image, 5);

            // 转换图像为灰度
            Mat grayImage = new Mat();
            CvInvoke.CvtColor(image, grayImage, ColorConversion.Bgr2Gray);

            // 使用霍夫变换检测圆
            CircleF[] circles = CvInvoke.HoughCircles(
                grayImage,
                HoughModes.Gradient,
                2.0, // 累加器分辨率
                200, // 最小圆的中心距离
                param1: 200, // Canny 边缘检测的高阈值
                param2: 200, // 圆心检测阈值
                minRadius: 0, // 最小圆半径
                maxRadius: 0 // 最大圆半径
            );

            /*
                [DisplayColor(255, 0, 0)]
                public double Blue
                [DisplayColor(0, 255, 0)]
                public double Green
                [DisplayColor(0, 0, 255)]
                public double Red
             */

            circles = circles.OrderBy(e => e.Center.X).ToArray();

            // 绘制检测到的圆，只绘制红色圆心的圆
            foreach (CircleF circle in circles)
            {
                // 绘制圆心
                CvInvoke.Circle(image, Point.Round(circle.Center), (int)circle.Radius , new MCvScalar(0, 255, 0), 2);

                // 绘制十字标志
                int crossLength = 10; // 十字标志长度
                Point crossCenter = Point.Round(circle.Center);
                CvInvoke.Line(image, new Point(crossCenter.X - crossLength, crossCenter.Y), new Point(crossCenter.X + crossLength, crossCenter.Y), new MCvScalar(0, 255, 255), 2);
                CvInvoke.Line(image, new Point(crossCenter.X, crossCenter.Y - crossLength), new Point(crossCenter.X, crossCenter.Y + crossLength), new MCvScalar(0, 255, 255), 2);

                // 长方形的宽和高
                //int rectangleWidth = 260;
                //int rectangleHeight = 50;

                int rectangleWidth = (int)(circle.Radius * 0.65d);
                int rectangleHeight = (int)(circle.Radius * 0.171d);

                Console.WriteLine($"{circle.Radius}");

                // 计算矩形的左上角和右下角坐标
                int topLeftX = (int)circle.Center.X - rectangleWidth / 2;
                int topLeftY = (int)circle.Center.Y - (int)(circle.Radius * 0.45) - rectangleHeight / 2;
                int bottomRightX = (int)circle.Center.X + rectangleWidth / 2;
                int bottomRightY = (int)circle.Center.Y - (int)(circle.Radius * 0.45) + rectangleHeight / 2;

                // X - 一个偏移量，并没有完全中心位子
                topLeftX -= (int)(circle.Radius * 0.12d);

                // 确保矩形在图像范围内
                if (topLeftX < 0)
                    topLeftX = 0;
                if (topLeftY < 0)
                    topLeftY = 0;
                if (bottomRightX > image.Width)
                    bottomRightX = image.Width;
                if (bottomRightY > image.Height)
                    bottomRightY = image.Height;

                // 定义感兴趣的区域的矩形坐标
                Rectangle roi = new Rectangle(topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY);

                // 提取感兴趣的区域
                Mat regionOfInterest = new Mat(image, roi);



                // 转换 byte 给Tesseract使用
                #region  转换 byte 给Tesseract使用

                // 将 Mat 转换为 Image
                Image<Bgr, byte> img = regionOfInterest.ToImage<Bgr, byte>();
                // 将 Image 转换为 byte[]
                byte[] byteArray = img.ToJpegData();
                FindTesseract.Find(byteArray);
                #endregion

                // 显示提取的区域
                CvInvoke.Imshow("ROI", regionOfInterest);
                CvInvoke.WaitKey(0); // 等待按键退出
            }


            

            //// 显示结果
            //CvInvoke.Imshow("Detected Circles", image);
            //CvInvoke.WaitKey(0);
        }

        //public static Mat Change(Mat image)
        //{
        //    return ImageSegmentationHelper.Watershed((Bitmap)image.ba);
        //}
    }
}
